#ifndef CAMERAPAGE_H
#define CAMERAPAGE_H

#include <QObject>
#include <QWidget>
#include <QStackedWidget>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QString>
#include <QGraphicsOpacityEffect>
#include <QMessageBox>
#include <KWindowEffects>
#include <KWindowSystem>
#include <QPushButton>
#include <QIcon>
#include <QPropertyAnimation>
#include <QPainter>
#include <QBitmap>
#include <gsettings.hpp>
#include <QMouseEvent>

#include "windowmanager/windowmanager.h"
#include "utils/xatom-helper.h"
#include "KylinCamera.h"
#include "VideoDisplay.h"
#include "VideoDisplayGL.h"
#include "camera/currentdeviceinfo.h"
#include "camera/devicepull.h"
#include "camera/deviceread.h"
#include "global/global.h"
#include "utils/screencapsaver.h"
#include "utils/confighandler.h"

class CameraPage : public QStackedWidget
{
    Q_OBJECT
public:
    enum SideType {     //选框的不同侧边或角
        TOPLEFT_SIDE,
        BOTTONLEFT_SIDE,
        TOPRIGHT_SIDE,
        BOTTONRIGHT_SIDE,
        TOP_SIDE,
        BOTTON_SIDE,
        RIGHT_SIDE,
        LEFT_SIDE,
        NO_SIDE,
        Button_SIDE,
    };

    explicit CameraPage(QWidget *parent = nullptr, QRect recordingRect = QRect());
    void stopCamera();              //关闭摄像头
    void updateDisplayMask();       //摄像头圆角蒙版

    SideType getMouseSide(const QPoint &point) const;   //获取鼠标在选框的位置
    bool getCameraWidChanged();                         //获取属性-选框改变
    void setCameraWidChanged(bool ischanged);           //设置属性-选框改变
    void saveGeometry();                                //保存选框
    QRect savedGeometry();                              //获取已保存选框

    void UpdateResolution(V4l2DeviceInfo *device);

    //录制时移动和调节大小
    QRect m_recordingRect;

signals:
    void animationEnded();
    void setFontSizeSignal(int); //字体变化

    void swithToHasDevicePage();
    void swithToNoDevicePage(int);//1：无设备 2：编码格式不支持
    void lockSigForDevicePull(bool);

protected:
    void resizeEvent(QResizeEvent * e) override;
    void moveEvent(QMoveEvent *e)override;
    void mousePressEvent(QMouseEvent * e) override;
    void mouseMoveEvent(QMouseEvent * e) override;
    void mouseReleaseEvent(QMouseEvent * e) override;

private slots:
    void pullOut();
    void cameraChanged(QString DeviceNamePath);

private:
    void initLayout();                                                      //初始化布局
    void haveDevicesInit();
    void noDevicesInit();
    void initConnect();
    //创建摄像头
    KylinCameraInfo cameraInfoSet(const char *in_devname,QString scale);    //设置摄像头信息
    void creatCameraPage(const char *in_devname);                           //创建摄像头页面
    //更改摄像头
    void change_device(const char *in_device);          //切换摄像头
    bool cameraInfoSetTmp(const char *in_devname);      //探测
    void startExistCamera(KylinCameraInfo cameraInfo);
    void exception_handing();                           //拔出摄像头的异常处理
    //切换显示摄像头页面
    void displayCamera();
    void displayCameraError(int code);

    //主题和字体变化
    void initFontSize();
    void lable2SetFontSizeSlot(int size);

    KylinCamera *camera = nullptr;              //摄像头
    KylinCameraInfo m_camInfo;                  //摄像头信息
    VideoDisplayGL *videoDisplay = nullptr;     //摄像头显示
    DevicePull * devicePull = nullptr;          //设备插拔
    QString scale;                              //分辨率
    bool has_device = false;                    //是否有设备
    QGraphicsOpacityEffect *m_pGraphicsOpacityEffect1 = nullptr;    //图形透明度效果

    int m_width = 0;
    int m_height = 0;

    QWidget *haveDevicePage = nullptr;
    QStackedLayout * hasLayout = nullptr;
    QWidget *noDevicePage = nullptr;            //无摄像头页面
    QLabel *labelText  = nullptr;
    QLabel *labelIcon  = nullptr;

    //调整大小
    QColor m_color;
    QPropertyAnimation *m_animation;
    QPoint m_areaOffset;
    QPoint m_handleOffset;
    QRect m_TLHandle, m_TRHandle, m_BLHandle, m_BRHandle; //可拖动拉伸选框的手柄 - 四角
    QRect m_LHandle, m_THandle, m_RHandle, m_BHandle;
    QRect m_TLArea, m_TRArea, m_BLArea, m_BRArea; //选框周围的阴影 - 四角
    QRect m_LArea, m_TArea, m_RArea, m_BArea;
    QRect m_geometryBackup;
    bool m_cameraChanged = false; //选框大小改变
    void updateAreas(); //更新选框

    //录制时移动和调节大小
    void updateCursor();
    void updateCameraPos(QPoint point);
    void updateCameraSize(QPoint point);

    QTimer * m_updateMouseSideTimer = nullptr;
    CameraPage::SideType m_mouseOverHandleCamera;    //鼠标在摄像头的位置
    bool isMousePress = false;
    bool isMouseRelease = false;
    bool isMoveCamera = false;
    bool isSizeCamera = false;
    QPoint m_dragStartPoint_g;

};

#endif // CAMERAPAGE_H
